package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.aspectj.weaver.ast.Var;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {
    @Autowired
    private MongoTemplate mongoTemplate;
    //添加状态
    @Override
    public Integer saveComment(Comment comment) {
        //将comment添加道数据库中
        mongoTemplate.save(comment);
        //根据type判断调用那个方法
        switch (comment.getCommentType()) {
            case 1:{
                //根据动态id从懂啊提表中查询动态
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //将该动态的点赞+1
                movement.setLikeCount(movement.getLikeCount()+1);
                //返回动态
                mongoTemplate.save(movement);
                //返回点赞数量
                return  movement.getLikeCount();
            }
            case 2:{
                //根据动态id从懂啊提表中查询动态
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setCommentCount(movement.getCommentCount()+1);
                mongoTemplate.save(movement);
                return movement.getCommentCount();
            }
            case 3:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setLoveCount(movement.getLoveCount()+1);
                mongoTemplate.save(movement);
                return movement.getLoveCount();
            }

        }

        return 0;
    }
//删除动态
    @Override
    public Integer removeComment(ObjectId publishId, Long userId, Integer commentType) {
        //创建动态查询条件
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("publishId").is(publishId)
                        .and("commentType").is(commentType)
        );
        //删除coment中的记录
        mongoTemplate.remove(query,Comment.class);
        //根据类型判断业务
        switch (commentType) {
            case 1:{
                //根据动态id从懂啊提表中查询动态
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //将该动态的点赞+1
                movement.setLikeCount(movement.getLikeCount()-1);
                //返回动态
                mongoTemplate.save(movement);
                //返回点赞数量
                return  movement.getLikeCount();
            }
            case 2:{
                //根据动态id从懂啊提表中查询动态
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setCommentCount(movement.getCommentCount()-1);
                mongoTemplate.save(movement);
                return movement.getCommentCount();
            }
            case 3:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setLoveCount(movement.getLoveCount()-1);
                mongoTemplate.save(movement);
                return movement.getLoveCount();
            }

        }


        return 0;
    }
    //根据动态id获得评论信息
    @Override
    public PageBeanVo findByPage(String movementId, Integer pageNum, Integer pageSize, Integer i) {
        //创建查询条件
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(i)
        ).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);
        //查询conment表，评论都在这个表中
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        // 3.查询总记录数
        long total = mongoTemplate.count(query, Comment.class);
        // 4.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, total, commentList);
    }
    // 查看点赞、评论、喜欢的用户信息
    @Override
    public PageBeanVo findCommentTypeByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {
        Query query = Query.query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType)).with(Sort.by(
                Sort.Order.desc("created")
        )).skip((pageNum - 1) * pageSize).limit(pageSize);
        //根据条件查询、
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        // 3.查询total
        long total = mongoTemplate.count(query, Comment.class);
        // 4.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, total, commentList);
    }
}